﻿$(function() {
	/**
	 * 电度电费
	 * */
    var complexpie1 = echarts.init(document.getElementById('complexpie1'));
    optionpie1 = {
    		tooltip: {
		        trigger: 'item',
		        formatter: "{a} <br/>{b}: {c} ({d}%)"
		    },
		    backgroundColor:'#e0e0e0',
		    color:['#F3A43B','#999933','#9bca62'],
		    legend: {
		        orient: 'vertical',
		        x: 'right',
		        data:[]
		    },
		    series: [
		        {
		            name:'电费',
		            type:'pie',
		            radius: [0, '40%'],

		            label: {
		                normal: {
		                    position: 'inside'
		                }
		            },
		            labelLine: {
		                normal: {
		                    show: false
		                }
		            },
		            data:[]
		        },
		        {
		            name:'电度',
		            type:'pie',
		            radius: ['55%', '80%'],
		            data:[]
		        }
		    ]
    };
    complexpie1.setOption(optionpie1);
    
    /**
     * 每日电度情况'
     * */
    var complexbar = echarts.init(document.getElementById('complexbar'));
    optionBar = {
    		tooltip : {
    	        trigger: 'axis',
    	        axisPointer : {            
    	            type : 'shadow'        
    	        }
    	    },
    	    grid: {
	            top: '10%',
	            left: '4%',
	            right: '4%',
	            bottom: '20%',
	            containLabel: true
	        },
    	    legend: {
    	    	bottom: '30',
    	        data:[]
    	    },
    	    dataZoom:{
    	    	type:'slider',
    	    	start:0,
    	    	end:100,
    	    	labelFormatter: function (value) {
    	    	    return value; 
    	    	},
    	    	bottom:1
    	    },
    	    backgroundColor:'#e0e0e0',
    	    color:['#00b19d','#ff6264','#f7aa47','#999933','#003366'],
    	    xAxis:  {
            	type: 'category',
    	        data: [],
    	        splitLine:{
	            	show:false
	            }
    	    },
    	    yAxis: {
    	    	 type: 'value',
    	    	 name:'每日电度',
    	    	 axisLabel:{
    	    		 formatter: '{value}'
    	    	 },
    	    	 splitLine:{
 	            	show:false
 	            }
    	    },
    	    series: []
	};
    complexbar.setOption(optionBar);
    
    /**
     * 班次耗电情况
     * */
    var complexpie2 = echarts.init(document.getElementById('complexpie2'));
    optionpie2 = {
    		tooltip : {
                trigger: 'item',
                formatter: "{b} :{c}度 ({d}%)"
            },
            legend: {
                orient: 'horizontal',
                bottom: '30',
                data: []
            },
            backgroundColor:'#e0e0e0',
            color:['#00b19d','#ff6264','#f7aa47','#999933','#003366'],
            series : [{
                type: 'pie',
                radius : '60%',
                center: ['50%', '50%'],
                data:[],
                itemStyle: {
                    emphasis: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                },
                label: {
                    normal: {
    	                 formatter: '{b}\n{c}度'
    	            }
                }
            }]
    };
    complexpie2.setOption(optionpie2);
    
    /**
     * 班次电费情况
     * */
    var complexpie3 = echarts.init(document.getElementById('complexpie3'));
    optionpie3 = {
    		tooltip : {
                trigger: 'item',
                formatter: "{b} :{c}元({d}%)"
            },
            legend: {
                orient: 'horizontal',
                bottom: '30',
                data: []
            },
            backgroundColor:'#e0e0e0',
            color:['#00b19d','#ff6264','#f7aa47','#999933','#003366'],
            series : [{
                type: 'pie',
                radius : '60%',
                center: ['50%', '50%'],
                data:[],
                itemStyle: {
                    emphasis: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                },
                label: {
                    normal: {
    	                 formatter: '{b}\n{c}元'
    	            }
                }
            }]
    };
    complexpie3.setOption(optionpie3);
    
    var lineId=0;var equId=0;
    var	startTime="";
    var	endTime="";
    
    /**
     * slider滑动事件
     * */
    $('#ss').slider({
    	range: true,
        onComplete:function(value){//滑块值改变时触发
	       	startTime=moment(value[0]).format('YYYY-MM-DD');
	       	endTime=moment(value[1]).format('YYYY-MM-DD');
	       	getData();
       }
    });
    /**
     * 初始化
     * */
    var vm = new Vue({
	    el: '#itemData',
	    data:{item:[]}
	}); 

    var timess=[parseInt(moment().subtract(7, 'days').format("x")),parseInt(moment().format("x"))];
    $('#ss').slider('setValues',timess);
    var arr=$('#ss').slider('getValues');
    var time1=arr[0];
    var time2=arr[1];
    startTime=moment(time1).format("YYYY-MM-DD");
    endTime=moment(time2).format("YYYY-MM-DD");
    getData();
    function getData(){
    	$.post(basePath+"/single/singleComplex/electricity.json",{"lineId":lineId,"equId":equId,startTime:startTime,endTime:endTime},function(res){
    		vm.$set('item',res);
    		if(res.success){
//    			alert("正确的显示界面");
    			complexpie1.setOption({
    				legend:{
    					data:res.pieLendge
    				},
    				series:[{
    					name:'电费',
    					data:res.pieFValue
    				},{
    					name:'电度',
    					data:res.pieDValue
    				}]
    			});
    			var series=[];
    		    for(var i = 0;i<res.eachDayValue.length;i++){
    		        series.push({
    		            name: res.eachDayValue[i].name,
    		            stack: '总量',
    		            type: 'bar',
    		            barMaxWidth:40,
    		            data: res.eachDayValue[i].value
    		        });
    		    }
    		    complexbar.setOption({
    				legend: {
    		            data: res.ledge
    		        },
    				xAxis: {
			            data: res.days
			        },
			        series:series
    			});
    		    complexpie2.setOption({
    				legend: {
    		            data: res.ledge
    		        },
    		        series: [{
			            data: res.totalCompByShift
			        }]
    			});
    		    complexpie3.setOption({
    				legend: {
    		            data: res.ledge
    		        },
    		        series: [{
			            data: res.totalChargeByShift
			        }]
    			});
    		}else{
    			alert(res.msg);
    		}
    	});
    }
    
    /**
     * 自定义时间按钮的点击事件
     * */
	 $("#defineTime").click(function (){
		 var mePanel = $('#defineTimeWin');
	   	 mePanel.dialog('open');
	 });
	 
	 /**
	  * 切换设备的点击事件
	  * */
	var mePanel = $('#switchEquip');
	$("#linetree").tree({
		onClick: function(node) {
			if (node.url) {
				lineId=node.id;
				var lineGrid = $('#lines_data');
				lineGrid.datagrid('load',{
					lineId:node.id
				});
				lineGrid.datagrid('getPanel').panel('setTitle',node.text);
			}
		}
	});
	$('#lines_data').datagrid({
 		//双击设备时，根据选中设备刷新
 		onDblClickRow: function(rowIndex, rowData){
 			lineId=rowData.lineId;
 			equId=rowData.id;
 			var arr=$('#ss').slider('getValues');
 		    var time1=arr[0];
 		    var time2=arr[1];
 		    startTime=moment(time1).format("YYYY-MM-DD");
 		    endTime=moment(time2).format("YYYY-MM-DD");
 		    getData();
 		    mePanel.dialog('close');
 		}
	});
   $("#switchEquipOK").click(function(){
   	var row = $('#lines_data').datagrid('getSelected');
   	if(row){
   		lineId=row.lineId;
   		equId=row.id;
   		var arr=$('#ss').slider('getValues');
   	    var time1=arr[0];
   	    var time2=arr[1];
   	    startTime=moment(time1).format("YYYY-MM-DD");
   	    endTime=moment(time2).format("YYYY-MM-DD");
   	    getData();
		$('#switchEquip').dialog('close');
   	}else{
   		$.messager.alert({showSpeed:100,title:'消息提示',msg:'请选择一条设备记录!',timeout:3000});
   	}
   });

});